﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Entities;
using CoVanHocTapData.Infrastructure;
using CoVanHocTapData.Interface;

namespace CoVanHocTapData.Concretes
{
    public class ChuongTrinhDaoTaoRepository : IChuongTrinhDaoTaoRepository
    {
        DangKyPhuEntities entities;
        public ChuongTrinhDaoTaoRepository()
        {
            entities = new DangKyPhuEntities();
        }

        #region repos
        INienKhoaRepository nienKhoaRepos = new NienKhoaRepository();
        #endregion

        #region IChuongTrinhDaoTaoRepository Members

        /// <summary>
        /// Lay chuong trinh dao tao theo nganh, nien khoa, he dao tao
        /// </summary>
        /// <param name="NganhID"></param>
        /// <param name="NienKhoaID"></param>
        /// <param name="HeDaoTaoID"></param>
        /// <returns></returns>
        public IQueryable<ChuongTrinhDaoTao> LayChuongTrinhDaoTao(string NganhID, int NienKhoaID, string HeDaoTaoID)
        {

            var result = (from m in entities.ChuongTrinhDaoTaos
                    where m.NganhID.Equals(NganhID) && m.NienKhoaID == NienKhoaID
                    && m.HeDaoTaoID.Equals(HeDaoTaoID)
                    select m);
            if(result==null || result.Count()==0)
                return (from m in entities.ChuongTrinhDaoTaos
                        where m.NganhID.Equals(NganhID) && m.NienKhoaID == 0
                        && m.HeDaoTaoID.Equals(HeDaoTaoID)
                        select m);
            return result;
        }

        /// <summary>
        /// Lay MonHocID trong ctdt
        /// </summary>
        /// <param name="NganhID"></param>
        /// <param name="NienKhoaID"></param>
        /// <param name="HeDaoTaoID"></param>
        /// <returns></returns>
        public IQueryable<string> LayMonHocIDChuongTrinhDaoTao(string NganhID, int NienKhoaID, string HeDaoTaoID)
        {
            var result = (from m in entities.ChuongTrinhDaoTaos
                          where m.NganhID.Equals(NganhID) && m.NienKhoaID == NienKhoaID
                          && m.HeDaoTaoID.Equals(HeDaoTaoID)
                          select m.MonHocID);
            if (result == null || result.Count() == 0)
                //return (from m in entities.ChuongTrinhDaoTaos
                //        where m.NganhID.Equals(NganhID) && m.NienKhoaID == 0
                //        && m.HeDaoTaoID.Equals(HeDaoTaoID)
                //        select m.MonHocID);
                return LayMonHocIDChuongTrinhDaoTao(NganhID, 0, HeDaoTaoID);
            return result;
        }

        public IQueryable<ChuongTrinhDaoTao> LayChuongTrinhDaoTaoChuan(string NganhID, int NienKhoaID, string HeDaoTaoID)
        {
            var result = (from m in entities.ChuongTrinhDaoTaos
                    where m.NganhID.Equals(NganhID) && 
                    (m.NienKhoaID == NienKhoaID)
                    && m.HeDaoTaoID.Equals(HeDaoTaoID) && (m.TuChon==false || m.TuChon==null)
                    select m);
            if(result==null || result.Count()==0)
                return  (from m in entities.ChuongTrinhDaoTaos
                 where m.NganhID.Equals(NganhID) &&
                 (m.NienKhoaID == 0)
                 && m.HeDaoTaoID.Equals(HeDaoTaoID) && m.TuChon == false
                 select m);
            return result;
        }

        
        /// <summary>
        /// Lay het chuong trinh dao tao
        /// </summary>
        /// <returns></returns>
        public IQueryable<ChuongTrinhDaoTao> LayChuongTrinhDaoTao()
        {
            return (from m in entities.ChuongTrinhDaoTaos
                    select m);
        }

        

        #endregion



        #region IChuongTrinhDaoTaoRepository Members


        public IQueryable<ChuongTrinhDaoTao> LayChuongTrinhDaoTaoTheoHocKy(string NganhID, int NienKhoaID, string HeDaoTaoID, string HocKy)
        {
            var result = from m in entities.ChuongTrinhDaoTaos
                         where m.NganhID.Equals(NganhID)
                         && m.NienKhoaID == NienKhoaID
                         && m.HeDaoTaoID.Equals(HeDaoTaoID)
                         && m.HocKy.Equals(HocKy)
                         select m;
            if (result == null || result.Count() == 0)
            {
                return from m in entities.ChuongTrinhDaoTaos
                       where m.NganhID.Equals(NganhID)
                       && m.NienKhoaID == 0
                       && m.HeDaoTaoID.Equals(HeDaoTaoID)
                       && m.HocKy.Equals(HocKy)
                       select m;
            }
            return result;
        }

        #endregion
    }
}
